package svc

import (
	"github.com/zeromicro/go-zero/core/stores/redis"
	"github.com/zeromicro/go-zero/rest"
	"github.com/zeromicro/go-zero/zrpc"
	"zero-admin/rpc/sys/sys"
	"zero-admin/sys-api/internal/config"
	"zero-admin/sys-api/internal/middleware"
)

type ServiceContext struct {
	Config   config.Config
	CheckUrl rest.Middleware
	Sys      sys.Sys
	Redis    *redis.Redis
}

func NewServiceContext(c config.Config) *ServiceContext {
	newRedis := redis.New(c.Redis.Address, redisConfig())
	return &ServiceContext{
		Config:   c,
		CheckUrl: middleware.NewCheckUrlMiddleware(newRedis).Handle,
		Sys:      sys.NewSys(zrpc.MustNewClient(c.SysRpc)),
		Redis:    newRedis,
	}
}

func redisConfig() redis.Option {
	return func(r *redis.Redis) {
		r.Type = redis.NodeType
	}
}
